/**
 *
 *  Copyright (c) 2011, AstroDAbis
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met:
 *
 *      * Redistributions of source code must retain the above copyright notice,
 *        this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright notice,
 *        this list of conditions and the following disclaimer in the documentation
 *        and/or other materials provided with the distribution.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 *  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
package org.jiscinvolve.astrodabis.galium.ogsadai;

import java.net.URL;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import java.util.Vector;

import org.junit.Test;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;  

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

//import uk.org.ogsadai.client.toolkit.example.SQLClient;

import uk.org.ogsadai.resource.ResourceID;
import uk.org.ogsadai.resource.request.RequestStatus;

import uk.org.ogsadai.client.toolkit.Server;
import uk.org.ogsadai.client.toolkit.ServerProxy;
import uk.org.ogsadai.client.toolkit.PipelineWorkflow;

import uk.org.ogsadai.client.toolkit.RequestResource;
import uk.org.ogsadai.client.toolkit.RequestExecutionType;

import uk.org.ogsadai.client.toolkit.DataRequestExecutionResource;
import uk.org.ogsadai.client.toolkit.activities.sql.SQLQuery;
import uk.org.ogsadai.client.toolkit.activities.delivery.DeliverToRequestStatus;
import uk.org.ogsadai.client.toolkit.activities.transform.TupleToByteArrays;

import org.jiscinvolve.astrodabis.galium.ogsadai.activity.client.CreateTapResource;
import org.jiscinvolve.astrodabis.galium.ogsadai.activity.client.ResultProcessing;

public class CreateTapServiceTestCase
    {
    /**
     * Our test service endpoint.
     * 
    public static final String endpoint = "http://beta.virtual.metagrid.co.uk:8080/dai/services/" ;
     */
    public static final String endpoint = "http://astro.metagrid.co.uk:8080/ogsadai/services/" ;

    /**
     * Our debug logger.
     * 
     */
    private static Log log = LogFactory.getLog(CreateTapServiceTestCase.class);

    /**
     * Test our CreateTapServiceActivity.
     *
     */
    @Test
    public void frog001()
    throws Exception
        {

        String tapurl   = "http://wfaudata.roe.ac.uk/ukidssDR5-dsa/TAP";
// http://wfaudata.roe.ac.uk/sdssdr7-dsa/TAP
// http://wfaudata.roe.ac.uk/ukidssDR5-dsa/TAP
        String adql     = "Select a.sourceID, a.ra, a.dec From gcsSource as a Where a.ra Between '56.28' And '56.30' And a.dec Between '50.60' And '50.62'";

        // Get server.
        Server server = new ServerProxy();
        server.setDefaultBaseServicesURL(
            new URL(
                endpoint
                )
            );

        // Get DRER.
        DataRequestExecutionResource drer = server.getDataRequestExecutionResource(
            new ResourceID(
                "DataRequestExecutionResource"
                )
            );

        // TAP service.
        CreateTapResource creator = new CreateTapResource();
        creator.addTapEndpoint(
            tapurl
            );

        // Deliver.
        DeliverToRequestStatus delivery = new DeliverToRequestStatus();
        delivery.connectInput(
            creator.getResultOutput()
            );


        // Workflow.
        PipelineWorkflow pipeline = new PipelineWorkflow();
        pipeline.add(creator);
        pipeline.add(delivery);

        // Execute.
        RequestResource requestResource = null;
        try {
            requestResource = drer.execute(
                pipeline,
                RequestExecutionType.SYNCHRONOUS
                );
            }

        catch (Throwable ouch)
            {
            log.error("Exception executing query");

            while (ouch != null)
                {
                log.error("Exception [" + ouch.getClass().getName() + "][" + ouch.getMessage() + "]");
                ouch = ouch.getCause();
                }
            }

        // Get request status.
        RequestStatus status = requestResource.getRequestStatus();
        log.debug("Status [" + status.getExecutionStatus() + "]");
        log.debug("Result [" + creator.nextResult() + "]");

        }
    }
